package com.example.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entry.OrderForm;
import com.example.service.IOrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
 * 订单后台管理控制器
 * */
@RequestMapping("/admin/order")
@RestController
public class OrderFormController {

    @Autowired
    private IOrderFormService orderFormService;

    /*
     * 根据id查询信息
     * */
    @GetMapping("/order-id={id}")
    public Result getOrderOne(@PathVariable Integer id){
        OrderForm byId = orderFormService.getById(id);
        return Result.success(byId);
    }

    /*
     * 根据id进行删除
     * */
    @GetMapping("/delete/id={id}")
    public Result deleteOrderInfo(@PathVariable Integer id) {
        orderFormService.removeById(id);
        return Result.success("删除成功");
    }

    /*
     * 批量删除
     * */
    @PostMapping("/batchd_delete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        orderFormService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    /*
     * 修改或保存信息
     * */
    @PostMapping("/update")
    public Result updateOrSavOrder(@RequestBody OrderForm orderForm){
        orderFormService.saveOrUpdate(orderForm);
        return Result.success("更新成功");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public Result getPageOrder(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") Integer status,
                               @RequestParam(defaultValue = "") String nickName,
                               @RequestParam(defaultValue = "") String userPhone,
                               @RequestParam(defaultValue = "") String riderName,
                               @RequestParam(defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                               @RequestParam(defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime){
        QueryWrapper<OrderForm> queryWrapper = new QueryWrapper<>();
        // 根据id进行降序排序
        queryWrapper.orderByDesc("id");
        // 查询管理员和普通用户
        if(Objects.nonNull(status)){
            queryWrapper.like("status", status);
        }
        // 根据条件进行模糊查询
        if(!"".equals(nickName)){
            queryWrapper.like("nick_name", nickName);
        }
        if(!"".equals(userPhone)){
            queryWrapper.like("user_phone", userPhone);
        }
        if(!"".equals(riderName)){
            queryWrapper.like("rider_name", riderName);
        }

        if(Objects.nonNull(startTime) && Objects.nonNull(endTime)){
            queryWrapper.between("create_time",startTime, endTime);
        }
        // 分页构造器
        Page<OrderForm> page = orderFormService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}
